package View;

//import java.awt.Dimension;
//import javax.swing.BoxLayout; // old layout
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
import Model.TrafficLight;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Simon Marshall
 * @author jc209673
 */
public class MyFrame extends JFrame { //implements ActionListener {

    private Display display;
    private JSlider jslider;
    private JComboBox jcombobox;
    private JButton jbutton;
    private JLabel HSliderValue;
    private JLabel VSliderValue;
    private JLabel SimCycleValue;
    private TrafficLight horizontalLight;
    private TrafficLight verticalLight;
    private ChangeListener HSliderListener = new ChangeListener() {
// Watches for a change in the slider and updates the label dynamically to reflect the change

        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            HSliderValue.setText("" + source.getValue());
        }
    };
    private ChangeListener VSliderListener = new ChangeListener() {
// Watches for a change in the slider and updates the label dynamically to reflect the change

        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            VSliderValue.setText("" + source.getValue());
        }
    };
    private ChangeListener SimCycleListener = new ChangeListener() {
// Watches for a change in the slider and updates the label dynamically to reflect the change

        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            SimCycleValue.setText("" + source.getValue());
        }
    };

    /**
     * Adds a JPanel to the bottom of the JFrame and adds Labels, Comboboxes, Sliders and Event Handlers
     */
    public MyFrame() {


        // populate 1st and 2nd comboboxes using an uneditable combobox
        //http://download.oracle.com/javase/tutorial/index.html
        //http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html#uneditable
        //http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

        setTitle("Traffic GUI Simulator Version 5.00a");
        setAlwaysOnTop(true);
        setVisible(true);

        display = new Display();
        add(display, BorderLayout.CENTER);

        JPanel MenuPanel = new JPanel();
        this.getContentPane().add(MenuPanel, BorderLayout.SOUTH);

        //MenuPanel.setLayout(new BoxLayout(MenuPanel, BoxLayout.Y_AXIS)); // use a gridlayout 7 columns 2 rows // old Panel layout
        MenuPanel.setLayout(new GridLayout(7, 2));

        //String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"}; //example of an uneditable combobox
        //Create the combo box, select item at index 4.
        //Indices start at 0, so 4 specifies the pig.
        //JComboBox petList = new JComboBox(petStrings);
        //petList.setSelectedIndex(4);
        //petList.addActionListener(this);

        //jcombobox = new JComboBox(); // Menu Option 1 - 1, 2 or 3 as choices with 2 as default.
        JPanel HorLanePanel = new JPanel();
        String[] optionOneStrings = {"1", "2", "3"};
        MenuPanel.add(new JLabel("How many Horizontal Lanes? [min 1, max 3]"));
        jcombobox = new JComboBox(optionOneStrings);
        jcombobox.setSelectedIndex(1);
        //jcombobox.addActionListener((ActionListener) this);
        MenuPanel.add(HorLanePanel);
        HorLanePanel.add(jcombobox);

        JPanel VerLanePanel = new JPanel();
        JComboBox jcombobox1 = new JComboBox(); // Menu Option 2 - 1, 2, 3 or 4 as choices with 2 as default
        String[] optionTwoStrings = {"1", "2", "3", "4"};
        MenuPanel.add(new JLabel("How many Vertical Lanes? [min 1, max 4]"));
        //jcombobox1 optionTwoStrings = new JComboBox(optionTwoStrings);
        jcombobox1 = new JComboBox(optionTwoStrings);
        jcombobox1.setSelectedIndex(1);
        //jcombobox1.addActionListener((ActionListener) this);
        MenuPanel.add(VerLanePanel);
        VerLanePanel.add(jcombobox1);

        JPanel VSliderPanel = new JPanel();
        JSlider jslidervertical = new JSlider(); // Menu Option 3 - 0.1 to 1.0 as choices with 0.5 as default
        jslidervertical.addChangeListener(VSliderListener);
        MenuPanel.add(new JLabel("Probability of a car entering Vertical Lane? [min 0%, max 100%]"));
        MenuPanel.add(VSliderPanel);
        VSliderValue = new JLabel("50");
        VSliderPanel.add(jslidervertical, BorderLayout.CENTER);
        VSliderPanel.add(VSliderValue);

        JPanel HSliderPanel = new JPanel();
        JSlider jsliderhorizontal = new JSlider(); // Menu Option 4 - 0.1 to 1.0 as choices with 0.5 as default
        jsliderhorizontal.addChangeListener(HSliderListener);
        MenuPanel.add(new JLabel("Probability of a car entering Horizontal Lane? [min 0%, max 100%]"));
        MenuPanel.add(HSliderPanel);
        HSliderValue = new JLabel("50");
        HSliderPanel.add(jsliderhorizontal, BorderLayout.CENTER);
        HSliderPanel.add(HSliderValue);

        JPanel OneCyclePanel = new JPanel();
        String[] optionFourStrings = {"Yes", "No"};
        JComboBox jcombobox4 = new JComboBox(); // Menu Option 5 - yes or no as choices
        MenuPanel.add(new JLabel("Run one simulation cycle?"));
        jcombobox4 = new JComboBox(optionFourStrings);
        jcombobox4.setSelectedIndex(1);
        //jcombobox4.addActionListener((ActionListener) this);
        MenuPanel.add(OneCyclePanel);
        OneCyclePanel.add(jcombobox4);

        JPanel SimCyclePanel = new JPanel();
        JSlider jslidersimulationcycle = new JSlider(); // Menu Option 6 - 1 to 100 as choices with 50 as default
        jslidersimulationcycle.addChangeListener(SimCycleListener);
        MenuPanel.add(new JLabel("Run number of simulation cycles [min 1, max 100]"));
        MenuPanel.add(SimCyclePanel);
        SimCycleValue = new JLabel("50");
        SimCyclePanel.add(jslidersimulationcycle, BorderLayout.CENTER);
        SimCyclePanel.add(SimCycleValue);

        //JButton jbuttonStart = new JButton(); // Start simulation
        //MenuPanel.add(new JLabel("Start Simulation"));
        JPanel StartButtonPanel = new JPanel();
        //MenuPanel.add(new JButton("Start Simulation"));
        MenuPanel.add(StartButtonPanel);
        StartButtonPanel.add(new JButton("Start Simulation"));
        //MenuPanel.add(jbuttonStart);

        //JButton jbuttonStop = new JButton(); // Stop simulation
        //MenuPanel.add(new JLabel("Stop Simulation"));
        JPanel StopButtonPanel = new JPanel();
        //MenuPanel.add(new JButton("Stop Simulation"));
         MenuPanel.add(StopButtonPanel);
        StopButtonPanel.add(new JButton("Stop Simulation"));
        //MenuPanel.add(jbuttonStop);

        //JButton jbuttonExit = new JButton(); // Exit simulation
        //MenuPanel.add(new JLabel("Exit Simulation"));
        //MenuPanel.add(new JButton("Exit Simulation"));
        //MenuPanel.add(jbuttonExit);

        //        jslider = new JSlider();
        //        jslider.addChangeListener(new ChangeListener() {
        //
        //            public void stateChanged(ChangeEvent e) {
        //                System.out.println(jslider.getValue());
        //            }
        //        });
        //
        //        MenuPanel.add(new JLabel("test"));
        //        MenuPanel.add(jslider);

        display.repaint();
        pack();
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    /**
     *
     * @param h creates a local variable for horizontalLight
     * @param v creates a local variable for verticalLight
     */
    public void setup(TrafficLight h, TrafficLight v) {
        display.setup(h, v);
        horizontalLight = h;
        verticalLight = v;
        

    }
    //public void actionPerformed(ActionEvent ae) {
    //System.out.println(ae.getActionCommand(ae.paramString())); //add something to identify which combobox option has being changed.
}
//}

